package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DefScoreRule;
import com.ruoyi.system.service.IDefScoreRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 得分规则Controller
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/system/rule")
public class DefScoreRuleController extends BaseController
{
    @Autowired
    private IDefScoreRuleService defScoreRuleService;

    /**
     * 查询得分规则列表
     */
    @GetMapping("")
    public TableDataInfo list(DefScoreRule defScoreRule)
    {
        //startPage();
        // List<DefScoreRule> list = defScoreRuleService.selectDefScoreRuleList(defScoreRule);
        if (defScoreRule.getDrillId() == null) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            defScoreRule.setDrillId(Long.valueOf(loginUser.getDrillId()));
        }
        return getDataTable(defScoreRuleService.ruleList(defScoreRule));
    }

    /**
     * 查询得分规则列表
     */
    @GetMapping("list")
    public AjaxResult list(Long pid)
    {
        DefScoreRule rule = new DefScoreRule();
        rule.setPid(pid);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        rule.setDrillId(Long.valueOf(loginUser.getDrillId()));
        List<DefScoreRule> list = defScoreRuleService.selectDefScoreRuleList(rule);
        return success(list);
    }

    /**
     * 导出得分规则列表
     */
    @Log(title = "得分规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DefScoreRule defScoreRule)
    {
        List<DefScoreRule> list = defScoreRuleService.selectDefScoreRuleList(defScoreRule);
        ExcelUtil<DefScoreRule> util = new ExcelUtil<DefScoreRule>(DefScoreRule.class);
        util.exportExcel(response, list, "得分规则数据");
    }

    /**
     * 获取得分规则详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(defScoreRuleService.selectDefScoreRuleById(id));
    }

    /**
     * 新增得分规则
     */
    @Log(title = "得分规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DefScoreRule defScoreRule)
    {
        return toAjax(defScoreRuleService.insertDefScoreRule(defScoreRule));
    }

    /**
     * 修改得分规则
     */
    @Log(title = "得分规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DefScoreRule defScoreRule)
    {
        return toAjax(defScoreRuleService.updateDefScoreRule(defScoreRule));
    }

    /**
     * 删除得分规则
     */
    @Log(title = "得分规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(defScoreRuleService.deleteDefScoreRuleByIds(ids));
    }

    @PostMapping("import")
    @Log(title = "得分规则", businessType = BusinessType.IMPORT)
    public AjaxResult importData(MultipartFile file, Integer scoreType) {
        return success(defScoreRuleService.importDefScoreRule(file, scoreType));
    }


}
